﻿using CompositionRoot;
using Core.ApplicationServices;
using Infrastructure.DomainServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace WSCSync
{
    public partial class Service1 : ServiceBase
    {
        private Timer timerSync;
        private IConfigurable config;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            var container = DependencyRegister.Register();
            config = container.GetInstance<IConfigurable>();
            timerSync = new Timer(config.TimeSyncElapsed * 1000); // every 1 minutes
            timerSync.Elapsed += new System.Timers.ElapsedEventHandler(timerSync_Elapsed);
            timerSync.Start();
        }

        private void timerSync_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timerSync.Stop();
            new BatchProcess().RunAll();
            timerSync.Start();
        }

        protected override void OnStop()
        {
            timerSync.Stop();
            timerSync = null;
        }
    }
}
